package com.linzhaowei.headhuntingservice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.easeui.EaseConstant;
import com.linzhaowei.headhuntingservice.bean.Applicant;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

public class JobObjectiveDetail extends AppCompatActivity implements View.OnClickListener {

    TextView name;
    TextView gender;
    TextView age;
    TextView education;
    TextView school;
    TextView major;
    TextView job_name;
    TextView salary;
    TextView address;
    TextView resume;
    Button chat;
    int id;

    JobObjective jobObjective;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_objective_detail);
        initViews();
        setEvents();
        id = getIntent().getIntExtra("job_objective_id", -1);
        getJobObjectiveDetail();
    }

    void initViews() {
        name = findViewById(R.id.j_o_d_name);
        gender = findViewById(R.id.j_o_d_gender);
        age = findViewById(R.id.j_o_d_age);
        education = findViewById(R.id.j_o_d_education);
        school = findViewById(R.id.j_o_d_school);
        major = findViewById(R.id.j_o_d_major);
        job_name = findViewById(R.id.j_o_d_job_name);
        salary = findViewById(R.id.j_o_d_salary);
        address = findViewById(R.id.j_o_d_address);
        resume = findViewById(R.id.j_o_d_resume);
        chat = findViewById(R.id.j_o_d_chat);
    }

    void setEvents() {
        chat.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.j_o_d_chat:
                Intent intent = new Intent(JobObjectiveDetail.this, Chat.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, jobObjective.getUser_name());
                startActivity(intent);
                break;
        }
    }

    void getJobObjectiveDetail() {
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                String result = httpUtils.get(Ip.ip + "/user/getJobObjectiveDetail?id=" + id);
                JSONArray jsonArray = JSON.parseArray(result);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                jobObjective = jsonObject.toJavaObject(JobObjective.class);
                show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    void show() {
        runOnUiThread(() -> {
            name.setText(jobObjective.getApplicant_name());
            gender.setText(jobObjective.getGender());
            age.setText(String.valueOf(jobObjective.getAge()));
            education.setText(jobObjective.getEducation());
            school.setText(jobObjective.getSchool());
            major.setText(jobObjective.getMajor());
            job_name.setText(jobObjective.getName());
            salary.setText(String.valueOf(jobObjective.getSalary()));
            address.setText(jobObjective.getAddress());
            resume.setText(jobObjective.getResume());
        });
    }
}
